package com.zqxq.lottery.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.bo.BaseBO;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.o2o.sys.SysBannerO2O;
import com.zqxq.lottery.entity.po.sys.SysBannerPO;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.sys.SysBannerVO;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.sys.ISysBannerService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@RequestMapping("/sysBanner")
public class SysBannerController extends SysBaseController{

    @Autowired
    private ISysBannerService sysBannerService;

    @Autowired
    private YmlConfig ymlConfig;
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @ApiOperation("获取分页列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysBannerVO>> queryPageList(@RequestBody BaseBO baseBO){
        Result result = new Result();
        try{
            Page<SysBannerPO> page = new Page(baseBO.getPageNo(), baseBO.getPageSize());
            Wrapper wrapper = new EntityWrapper();
            wrapper.orderBy("sort", false);
            page = sysBannerService.selectPage(page,wrapper);
            Page<SysBannerVO> voPage = SysBannerO2O.toPageVO(page);
            result.setData(voPage);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }

        return result;
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Result save(@RequestBody SysBannerVO vo){
        Result result = new Result();
        try{
            SysBannerPO po = SysBannerO2O.toPO(vo);
            if(vo.getId() == null){
                Long id = ymlConfig.getPkId();
                po.setId(id);
            }
            sysBannerService.insertOrUpdate(po);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

    @ApiOperation("保存")
    @PostMapping("/saveClickNum")
    public Result saveClickNum(Long id){
        Result result = new Result();
        try{
            sysBannerService.updateClientNum(id, 1);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }

        return result;
    }

    @ApiOperation("获取列表")
    @PostMapping("/queryList")
    public Result<Page<SysBannerVO>> queryList(String appKey,Integer type){
        Result result = new Result();
        try{
            Wrapper wrapper = new EntityWrapper();
            wrapper.orderBy("sort", false);
            wrapper.eq("status",1);
            wrapper.eq("type",type);
            if (!StringUtils.isEmpty(appKey)) {
                SysConfigPO mjConfig = sysConfigMapper.getByKey("APP_KEY");
                Map map = JSONObject.parseObject(mjConfig.getCval(), Map.class);
                String value = String.valueOf(map.get(appKey));
                if (value.equals("0")) {
                    wrapper.eq("title","马甲包banner");
                }else{
                    wrapper.ne("title","马甲包banner");
                }
            }else{
                wrapper.ne("title","马甲包banner");
            }
            List<SysBannerPO> poList = sysBannerService.selectList(wrapper);
            List<SysBannerVO> voList = SysBannerO2O.toVOList(poList);
            result.setData(voList);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

//    @ApiOperation("热门banner")
//    @PostMapping("/hot")
//    public Result<SysBannerVO> queryHot(){
//        Result result = new Result();
//        try{
//            Wrapper wrapper = new EntityWrapper();
//            wrapper.orderBy("sort", false);
//            wrapper.eq("status",1);
//            wrapper.eq("type","1");
//            List<SysBannerPO> poList = sysBannerService.selectList(wrapper);
//            if(poList.size()>0){
//                SysBannerVO data = SysBannerO2O.toVO(poList.get(0));
//                result.setData(data);
//            }
//        } catch (Exception e){
//            log.error("SysBannerController queryHot Error", e);
//        }
//        return result;
//    }
//
//    @ApiOperation("个人中心banner")
//    @PostMapping("/center")
//    public Result<SysBannerVO> queryCenter(){
//        Result result = new Result();
//        try{
//            Wrapper wrapper = new EntityWrapper();
//            wrapper.orderBy("sort", false);
//            wrapper.eq("status",1);
//            wrapper.eq("type","2");
//            List<SysBannerPO> poList = sysBannerService.selectList(wrapper);
//            if(poList.size()>0){
//                SysBannerVO data = SysBannerO2O.toVO(poList.get(0));
//                result.setData(data);
//            }
//        } catch (Exception e){
//            log.error("SysBannerController queryHot Error", e);
//        }
//        return result;
//    }
//
//    @ApiOperation("弹窗banner")
//    @PostMapping("/popup")
//    public Result<SysBannerVO> queryPopup(){
//        Result result = new Result();
//        try{
//            Wrapper wrapper = new EntityWrapper();
//            wrapper.orderBy("sort", false);
//            wrapper.eq("status",1);
//            wrapper.eq("type","3");
//            List<SysBannerPO> poList = sysBannerService.selectList(wrapper);
//            if(poList.size()>0){
//                SysBannerVO data = SysBannerO2O.toVO(poList.get(0));
//                result.setData(data);
//            }
//        } catch (Exception e){
//            log.error("SysBannerController queryHot Error", e);
//        }
//        return result;
//    }

    @ApiOperation("删除banner")
    @PostMapping("/close")
    public Result closeBanner(Long id){
        Result result = new Result();
        if (id == null){
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setMsg(CodeEnum.FAIL_100002.getMsg());
            return result;
        }
        try {
            sysBannerService.deleteById(id);
        } catch (Exception e){
            log.error("SysBannerController close",e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }
}
